package com.lovo.bbs.bo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.naming.NamingException;

import com.lovo.bbs.dao.LinkDao;
import com.lovo.bbs.po.LinkPo;
import com.lovo.bbs.vo.LinkVo;

/**
 * 友情链接Bo
 * @author tiancen2001
 *
 */
public class LinkBo {
	 //操作类型
	 public static final int OPER_OF_INSERT=1;
	 public static final int OPER_OF_UPDATE=2;
	 public static final int OPER_OF_DELETE=3;
	/**
	 * 返回所有友情链接Vo列表
	 * @return
	 */
   public LinkBo(){
	   
   }
   public  ArrayList<LinkVo> getAllLink() {
	   ArrayList<LinkVo> vos = new ArrayList<LinkVo>();
	   ArrayList<LinkPo> pos=null;
	try {
		pos = new LinkDao().getAllLink();
	} catch (NamingException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	   Iterator<LinkPo> it=pos.iterator();
	   while(it.hasNext()){
		   LinkPo po = it.next(); 
		   LinkVo vo = new LinkVo();
		   
		   vo.setLinkid(po.getLinkid());
		   vo.setLinkname(po.getLinkname());
		   vo.setAboutLink(po.getAboutLink());
		   vo.setLinkurl(po.getLinkurl());
		   
		   vos.add(vo);
	   }
	   return vos;
   }
   /**
    * 返回指定链接
    * @return
    */
   public  LinkVo getLinkByID(int linkid){
	   LinkVo vo = new LinkVo();
	   LinkPo po  = null;
	   try {
		po = new LinkDao().getLinkByID(linkid);
	} catch (NamingException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	   
	   vo.setLinkid(po.getLinkid());
	   vo.setLinkname(po.getLinkname());
	   vo.setAboutLink(po.getAboutLink());
	   vo.setLinkurl(po.getLinkurl());
	   
	   return vo;
	   
   }
   
   /**
    * 链接控制台更新
    */
   public  int updateLink(LinkVo vo ,int operType){
	   int updated = 0;
       LinkPo po = new LinkPo();
       po.setLinkid(vo.getLinkid());
       po.setLinkname(vo.getLinkname());
       po.setLinkurl(vo.getLinkurl());
       po.setAboutLink(vo.getAboutLink());
       try {
		updated = new LinkDao().updateLink(po,operType);
	} catch (NamingException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	   return updated;
   }
}
